package com.mck.controller;

import com.mck.entity.FileInfo;
import com.mck.entity.uploadFile;
import com.mck.entity.Result;
import com.mck.service.UploadFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("/api/public/file")
@Slf4j
public class FileController {

    @Autowired
    private UploadFileService uploadFileService;

    /**
     * 文件获取
     */
    @GetMapping
    public Result getFileList() {
        log.info("获取文件");

        List<FileInfo> fileInfoList = uploadFileService.getFileList();
        return Result.success(fileInfoList);
    }

    /**
     * 获取指定文件
     */
    @PostMapping("/specific")
    public Result getFileSpecific(@RequestBody FileInfo file) {
        log.info("获取指定文件");

        List<FileInfo> fileInfo = uploadFileService.getFileSpecific(file);
        return Result.success(fileInfo);
    }

    /**
     * 文件上传
     */
    @PostMapping
    public Result uploadFile(@RequestParam("file") MultipartFile file,
                             @RequestParam("fileType") String fileType,
                             @RequestParam("userId") Integer userId) {
        // System.out.println(file);
        log.info("上传文件: {}", file);

        uploadFile newfile = new uploadFile(file, fileType, userId);
        uploadFileService.uploadFile(newfile);
        return Result.success();
    }

    /**
     * 文件删除
     */
    @DeleteMapping
    public Result deleteFile(@RequestParam List<Integer> id) {
        log.info("删除文件");

        uploadFileService.deleteFile(id);
        return Result.success();
    }

}
